package cn.iocoder.yudao.module.biz.controller.admin.company;

import cn.iocoder.yudao.framework.excel.core.util.poi.ExcelUtilRuoyi;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.biz.controller.admin.company.domain.SanBizCompanyInfo;
import cn.iocoder.yudao.module.biz.controller.admin.company.service.SanBizCompanyInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

@Tag(name = "管理后台 - 公司信息维护")
@RestController
@RequestMapping("/biz/company-info")
@Validated
public class BizCompanyInfoController {

    @Resource
    private SanBizCompanyInfoService companyInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建公司信息")
    @PreAuthorize("@ss.hasPermission('biz:company-info:create')")
    public CommonResult<Long> createCompanyInfo(@Valid @RequestBody SanBizCompanyInfo createReqVO) {
        createReqVO.setCreator(SecurityFrameworkUtils.getLoginUserId()+"");
        createReqVO.setCreateTime(new Date());
        companyInfoService.save(createReqVO);
        return success(1l);
    }

    @PutMapping("/update")
    @Operation(summary = "更新公司信息")
    @PreAuthorize("@ss.hasPermission('biz:company-info:update')")
    public CommonResult<Boolean> updateCompanyInfo(@Valid @RequestBody SanBizCompanyInfo updateReqVO) {
        updateReqVO.setUpdater(SecurityFrameworkUtils.getLoginUserId()+"");
        updateReqVO.setUpdateTime(new Date());
        companyInfoService.updateById(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除公司信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('biz:company-info:delete')")
    public CommonResult<Boolean> deleteCompanyInfo(@RequestParam("id") Long id) {
        companyInfoService.removeById(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除公司信息")
                @PreAuthorize("@ss.hasPermission('biz:company-info:delete')")
    public CommonResult<Boolean> deleteCompanyInfoList(@RequestParam("ids") List<Long> ids) {
        companyInfoService.removeBatchByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得公司信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('biz:company-info:query')")
    public CommonResult<SanBizCompanyInfo> getCompanyInfo(@RequestParam("id") Long id) {
        SanBizCompanyInfo companyInfo = companyInfoService.getById(id);
        return success(companyInfo);
    }

    @GetMapping("/page")
    @Operation(summary = "获得公司信息分页")
    @PreAuthorize("@ss.hasPermission('biz:company-info:query')")
    public CommonResult<IPage<SanBizCompanyInfo>> getCompanyInfoPage(@Valid SanBizCompanyInfo pageReqVO) {
        IPage<SanBizCompanyInfo> pageResult = companyInfoService.getPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出公司信息 Excel")
    @PreAuthorize("@ss.hasPermission('biz:company-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCompanyInfoExcel(@Valid SanBizCompanyInfo pageReqVO,
              HttpServletResponse response) throws IOException {
        QueryWrapper queryWrapper = new QueryWrapper(pageReqVO);
        List<SanBizCompanyInfo> list = companyInfoService.list(queryWrapper);
        ExcelUtilRuoyi<SanBizCompanyInfo> util = new ExcelUtilRuoyi<>(SanBizCompanyInfo.class);
        util.exportExcel(response, list, "公司信息");
    }

    @GetMapping("/list")
    @Operation(summary = "获得公司信息列表")
    @PreAuthorize("@ss.hasPermission('biz:company-info:query')")
    public CommonResult<List<SanBizCompanyInfo>> getCompanyInfoList(@Valid SanBizCompanyInfo pageReqVO) {
        QueryWrapper queryWrapper = new QueryWrapper(pageReqVO);
        List<SanBizCompanyInfo> list = companyInfoService.list(queryWrapper);
        return success(list);
    }

}